using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class move : MonoBehaviour
{
    private Transform trans;
    float startPos = 40;
    public float moveStep = -0.2f;
    bool finishedThis = false;

    public GameObject father;
    private GameObject child;
    private GameObject[] children;
    public int objectIdx = 0;

    // Start is called before the first frame update
    void Start()
    {
        this.gameObject.transform.Translate(0, 0, startPos);
        
        children = new GameObject[father.transform.childCount];

        for (objectIdx = objectIdx+1; objectIdx < children.Length; objectIdx++)
        {
            child = transform.GetChild(objectIdx).gameObject;
            child.SetActive(false);
        }

        child = transform.GetChild(0).gameObject;
    }

    // 复位之前的位置也恢复
    private void Reset()
    {
        startPos = 40;

        child.transform.Translate(0, 0, startPos);

        objectIdx = (objectIdx + 1) % children.Length;
        child = transform.GetChild(objectIdx).gameObject;
        child.SetActive(true);

    }
    // Update is called once per frame
    void Update()
    {
        startPos -= moveStep;
    
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            child.transform.Translate(-2, 0, (10 - startPos) * Time.deltaTime);
        } else if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            child.transform.Translate(2, 0, (10 - startPos) * Time.deltaTime);
        } else
        {
            child.transform.Translate(0f, 0f, (10 - startPos) *0.4f * Time.deltaTime);
        }
        

        // 如果碰撞了，过1秒随机出现下一个物体
        if (child.transform.position.z < -3)
        {
            Debug.Log("运动的是:" + child.name);
            child.SetActive(false);
            finishedThis = true;
            Reset();
        }
        
    }
}
